package com.wlstock.funi.request;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.zip.GZIPInputStream;

import com.android.volley.NetworkResponse;
import com.android.volley.Request.Method;
import com.wlstock.funi.utils.HttpUtils;

public class WenGuStraRequest implements ParseNetResponseListener {
	
	public NetWorkListener netWorkListener;
	
	public void request(String key,String startDate,String endDate, String upperIncome,String lowerIncome,String fallIncome,String stockHoldCount,String reqTag,NetWorkListener listener) {
		CustomStringRequest curRequest = new CustomStringRequest();
		curRequest.networkResponseListener = netWorkListener;
		curRequest.parseListener = this;
		HashMap<String, String> header = requestHeader();
		HashMap<String, String> params = requestParams(key,startDate,endDate,upperIncome,lowerIncome,fallIncome, stockHoldCount);
		curRequest.request(Method.GET, "http://search.10jqka.com.cn/", header, null, HttpUtils.getUrl("http://search.10jqka.com.cn/regression/back-test", params), reqTag);
	}
	
	private HashMap<String, String> requestHeader() {
		HashMap<String, String> header = new HashMap<String, String>();
		header.put("Accept", "application/json, text/javascript, */*; q=0.01");
		header.put("Accept-Encoding", "gzip, deflate");
		header.put("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
		header.put("Connection", "keep-alive");
		header.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		header.put("", "");
		return header;
	}
	
	private HashMap<String, String> requestParams(String key,String startDate,String endDate, String upperIncome,String lowerIncome,String fallIncome,String stockHoldCount) {
		HashMap<String, String> params = new HashMap<String, String>();
		try {
			String keyEncode = URLEncoder.encode(key, "utf-8");
			params.put("query", keyEncode);
			params.put("daysForSaleStrategy", "2,3,4,5,10,20,30");
			params.put("startDate", startDate!=null?startDate:"2014-01-01");
			params.put("endDate", endDate);
			params.put("fell", "0.001");
			params.put("upperIncome", upperIncome!=null?upperIncome:"10");
			params.put("lowerIncome", lowerIncome!=null?lowerIncome:"8");
			params.put("fallIncome", fallIncome!=null?fallIncome:"5");
			params.put("stockHoldCount", stockHoldCount!=null?stockHoldCount:"2");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return params;
	}

	@Override
	public String parseResponse(NetworkResponse response) {
		//问股中需要用到的
		ByteArrayInputStream bIn = new ByteArrayInputStream(response.data);
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		try {
			GZIPInputStream gzipInputStream = new GZIPInputStream(bIn);
			byte[] buffer = new byte[256];
			int n;
			while ((n = gzipInputStream.read(buffer)) > 0) {
				bOut.write(buffer, 0, n);
			}
			String s = bOut.toString();
			return s;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	
}
